using Gx.Common;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.Other;
using Gx.Model.Role;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKGoodsTypesBll
    {
        public List<LKGoodsTypes> GetLKGoodsTypes()
        {
            BaseDal<LKGoodsTypes> dal = new BaseDal<LKGoodsTypes>();
            var list = dal.GetAll().ToList();
            var parents = list.Where(x => x.ParentId == 0).ToList();

            List<LKGoodsTypes> treeGoodsTypes = new List<LKGoodsTypes>();
            foreach (var item in parents)
            {
                item.state = "open";
                item.children = GetChilds(list, item);
            }

            return parents;
        }

        public List<TreeView> GetTypeComboxTree()
        {
            List<TreeView> tvList = new List<TreeView>();
            BaseDal<LKGoodsTypes> dal = new BaseDal<LKGoodsTypes>();
            var list = dal.GetAll().ToList();
            var parents = list.Where(x => x.ParentId == 0).ToList();
            tvList.Add(new TreeView() { id = 0, text = "顶级", children = new List<TreeView>() });
            TreeView tv = null;
            foreach (var item in parents)
            {
                tv = new TreeView()
                {
                    id = item.Id,
                    text = item.TypeName,
                    children = new List<TreeView>()
                };
                GetTreeChilds(list, item, tv);
                tvList[0].children.Add(tv);
            }
            return tvList;
        }

        private void GetTreeChilds(List<LKGoodsTypes> list, LKGoodsTypes parent, TreeView tv)
        {
            List<LKGoodsTypes> newList = list.Where(x => x.ParentId == parent.Id).ToList();
            foreach (var item in newList)
            {
                TreeView newTv = new TreeView()
                {
                    id = item.Id,
                    text = item.TypeName,
                    children = new List<TreeView>(),
                };
                GetTreeChilds(list, item, newTv);
                tv.children.Add(newTv);
            }
        }

        private List<LKGoodsTypes> GetChilds(List<LKGoodsTypes> list, LKGoodsTypes parent)
        {
            List<LKGoodsTypes> childs = list.Where(x => x.ParentId == parent.Id).ToList();
            foreach (var item in childs)
            {
                item.children = new List<LKGoodsTypes>();
                item.children.AddRange(GetChilds(list, item));
            }
            return childs;
        }

        /// <summary>
        /// 新增商品类别
        /// </summary>
        /// <param name="lKGoodsTypes"></param>
        /// <returns></returns>
        public ReturnModel AddLKGoodsTypes(LKGoodsTypes lKGoodsTypes)
        {
            LKGoodsTypesDal dal = new LKGoodsTypesDal();

            //var any = dal.AnyLKGoodsTypesName(lKGoodsTypes.LevelName);
            //if (any)
            //{
            //    return new ReturnModel { Success = false, Msg = "客户等级名称已存在" };
            //}


            lKGoodsTypes.CreateTime = DateTime.Now;
            lKGoodsTypes.CreateUserId = LoginHelper.GetUserId();
            lKGoodsTypes.CreateUser = LoginHelper.UserName();
            var result = dal.Insert(lKGoodsTypes);
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑商品类别
        /// </summary>
        /// <param name="lKGoodsTypes"></param>
        /// <returns></returns>
        public bool EditLKGoodsTypes(LKGoodsTypes lKGoodsTypes)
        {
            BaseDal<LKGoodsTypes> dal = new BaseDal<LKGoodsTypes>();
            var cus = dal.Get(lKGoodsTypes.Id);
            if (cus == null)
            {
                return false;
            }
            lKGoodsTypes.CreateTime = cus.CreateTime;
            lKGoodsTypes.CreateUserId = cus.CreateUserId;
            lKGoodsTypes.CreateUser = cus.CreateUser;
            return dal.Update(lKGoodsTypes);
        }

        /// <summary>
        /// 删除商品类别
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKGoodsTypes(int id)
        {
            BaseDal<LKGoodsTypes> dal = new BaseDal<LKGoodsTypes>();
            return dal.DeleteById(id);
        }
    }
}

